package com.wyd.System.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyd.System.Bean.*;
import com.wyd.System.Mapper.*;
import com.wyd.System.Service.*;
import com.wyd.handler.BusinessException;
import com.wyd.response.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 学生信息表 服务实现类
 * </p>
 *
 * @author 文永达
 * @since 2020-12-02
 */
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements StudentsService {

    @Autowired(required = false)
    private StudentsMapper studentsMapper;

    @Autowired(required = false)
    private GradeMapper gradeMapper;

    @Autowired(required = false)
    private InstituteMapper instituteMapper;

    @Autowired(required = false)
    private SpecialtyMapper specialtyMapper;

    @Autowired(required = false)
    private ClazzMapper clazzMapper;

    @Override
    public List<Students> findStudentId(String studentId){
        return studentsMapper.findStudentId(studentId);
    }

    @Override
    public IPage<Students> findStudentPage(Page<Students> page, QueryWrapper<Students> wrapper) {
        return this.baseMapper.findStudentPage(page,wrapper);
    }

    @Override
    public void addStudent(Students students) {
        //判断是否增加了同一个学生
        String studentId = students.getStudentId();
        //获取年级
        BigDecimal grade_id = students.getGrade();
        //获取学院
        BigDecimal institute_id = students.getInstitute();
        //获取专业
        BigDecimal specialty_id = students.getSpecialty();
        //获取班级
        BigDecimal clazz_id = students.getStudentClazz();

        QueryWrapper<Students> wrapper = new QueryWrapper<>();
        wrapper.eq("STUDENT_ID",studentId);
        Integer count = this.baseMapper.selectCount(wrapper);
        if(count!=0){
            throw new BusinessException(ResultCode.STUDENT_ALREADY_EXIST.getCode(),
                    ResultCode.SPECIALTY_NOT_FOUND_EXCEPTION.getMessage());
        }
        //年级没有对应的处理
        Grade grade = gradeMapper.selectById(grade_id);
        if(grade==null){
            throw new BusinessException(ResultCode.GRADE_NOT_EXIST.getCode(),
                    ResultCode.GRADE_NOT_EXIST.getMessage());
        }
        //学院没有对应的处理
        Institute institute = instituteMapper.selectById(institute_id);
        if(institute==null){
            throw new BusinessException(ResultCode.INSTITUTE_NOT_EXIST.getCode(),
                    ResultCode.INSTITUTE_NOT_EXIST.getMessage());
        }
        //专业没有对应的处理
        Specialty specialty = specialtyMapper.selectById(specialty_id);
        if(specialty==null){
            throw new BusinessException(ResultCode.SPECIALTY_NOT_EXIST.getCode(),
                    ResultCode.SPECIALTY_NOT_EXIST.getMessage());
        }
        //班级没有对应的处理
        Clazz clazz = clazzMapper.selectById(clazz_id);
        if(clazz==null){
            throw new BusinessException(ResultCode.CLAZZ_NOT_EXIST.getCode(),
                    ResultCode.CLAZZ_NOT_EXIST.getMessage());
        }

        this.baseMapper.insert(students);
    }

    @Override
    public void deleteStudentById(BigDecimal id) {
        Students students = studentsMapper.selectById(id);

        if(students==null){
            throw new BusinessException(ResultCode.STUDENTS_CAN_NOT_DELETE.getCode(),
                    ResultCode.STUDENTS_CAN_NOT_DELETE.getMessage());
        }

        studentsMapper.deleteById(id);
    }

    @Override
    public Students editStudentById(BigDecimal id) {
        Students students = studentsMapper.selectById(id);
        if(students==null){
            throw new BusinessException(ResultCode.STUDENTS_CAN_NOT_EDIT.getCode(),
                    ResultCode.STUDENTS_CAN_NOT_EDIT.getMessage());
        }
        Grade grade = gradeMapper.selectById(students.getGrade());
        if(grade!=null){
            students.setGrade(grade.getId());
        }
        Institute institute = instituteMapper.selectById(students.getInstitute());
        if(institute!=null){
            students.setGrade(institute.getId());
        }
        Specialty specialty = specialtyMapper.selectById(students.getSpecialty());
        if(specialty!=null){
            students.setSpecialty(specialty.getId());
        }
        Clazz clazz = clazzMapper.selectById(students.getStudentClazz());
        if(clazz!=null){
            students.setStudentClazz(clazz.getId());
        }
        return students;
    }


}
